package com.baway.zhangcaiping;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/3/6.
 */

public class CustomView extends View {

    private int typeSize;
    private float intR;
    private float outR;
    private int ringColor;
    private String textContent;
    private Paint mpaint;
    private Rect mbound;
    Context context;
    Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            //跳转
            Intent intent=new Intent(context,Main2Activity.class);

            //开启跳转
            context.startActivity(intent);
        }
    };

    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        //获取属性集合
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);

        //获取属性
        typeSize = typedArray.getDimensionPixelSize(R.styleable.MyView_textSize, 18);
        intR = typedArray.getDimension(R.styleable.MyView_intR, 60);
        outR = typedArray.getDimension(R.styleable.MyView_outR, 100);
        ringColor = typedArray.getColor(R.styleable.MyView_ringColor, Color.YELLOW);
        textContent = typedArray.getString(R.styleable.MyView_textContent);

        //回收
        typedArray.recycle();

        //画笔
        mpaint = new Paint();

        //文本范围
        mbound = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画笔颜色
        mpaint.setColor(Color.GREEN);

        //绘制矩形
        canvas.drawRect(getWidth()/2-100,getHeight()/2-100,getWidth()/2+100,getHeight()/2+100,mpaint);

        //画笔颜色
        mpaint.setColor(ringColor);

        //绘制外圆
        canvas.drawCircle(getWidth()/2,getHeight()/2,outR,mpaint);

        //画笔颜色
        mpaint.setColor(Color.WHITE);

        //绘制内圆
        canvas.drawCircle(getWidth()/2,getHeight()/2,intR,mpaint);

        //画笔颜色
        mpaint.setColor(Color.BLACK);

        //文本大小
        mpaint.setTextSize(typeSize);

        //获取文本范围
        mpaint.getTextBounds(textContent,0,textContent.length(),mbound);

        //绘制文本
        canvas.drawText(textContent,getWidth()/2-mbound.width()/2,getHeight()/2+mbound.height()/2,mpaint);

    }

    //触摸点击事件

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:

                //获取点击的坐标
                float x = event.getX();
                float y = event.getY();

                //计算点击的坐标离圆点的距离
                float v = getWidth() / 2 - x;
                float v1 = getHeight() / 2 - y;
                float v2 = v * v + v1 * v1;
                double sqrt = Math.sqrt(v2);

                //判断
                if(sqrt<=intR)
                {
                    Toast.makeText(context,"在小圆内",Toast.LENGTH_SHORT).show();
                }else if(sqrt>intR&&sqrt<=outR)
                {
                    Toast.makeText(context,"在圆环内",Toast.LENGTH_SHORT).show();

                    //发送延迟消息
                    handler.sendEmptyMessageDelayed(1,2000);

                }

                break;
        }
        return true;
    }
}
